package com.hackcloud.shardingjdbc.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 模板的读取类
 *
 * @author Jiaju Zhuang
 */
@Slf4j
public class DocTemplateReadListener extends AnalysisEventListener<DocData> {
    private static final List<String> DOC_NODE = Arrays.asList("request", "response", "extInfo", "fileExtInfo");
    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 200;

    private Map<String, List<DocData>> propertyMap = new HashMap<>();

    private List<DocData> cachedDataList = new ArrayList<>();


    @Override
    public void invoke(DocData data, AnalysisContext context) {
        if (!StringUtils.isEmpty(data.getFieldSortNo())) {
            cachedDataList.add(data);
        }


    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        String key = "";
        List<DocData> docDataList = new ArrayList<>();
        for (int i = 0; i < cachedDataList.size(); i++) {

            DocData docData = cachedDataList.get(i);

            if (DOC_NODE.contains(docData.getFieldSortNo()) && CollectionUtils.isEmpty(docDataList)) {
                key = docData.getFieldSortNo();
            }
            if (DOC_NODE.contains(docData.getFieldSortNo()) && CollectionUtils.isNotEmpty(docDataList)) {
                propertyMap.put(key, docDataList.stream().collect(Collectors.toList()));
                docDataList.clear();
                key = docData.getFieldSortNo();
            }
//            if (docData.getFieldSortNo().split("\\.").length == 1 && ("body").equals(docData.getFieldCode())) {
//                continue;
//            }
            if (docData.getFieldSortNo().split("\\.").length == 1 && ("header").equals(docData.getFieldCode())) {
                docDataList.clear();
            }
            if (docData.getFieldSortNo().split("\\.").length == 1 && (DOC_NODE.contains(docData.getFieldSortNo())
                    || DOC_NODE.contains(docData.getFieldSortNo()))) {
                docDataList.clear();
                continue;
            }
            if (docData.getFieldSortNo().split("\\.").length == 1 && "序号".equals(docData.getFieldSortNo())) {
                docDataList.clear();
                continue;
            }


            if (docData.getFieldSortNo().split("\\.").length == 2 && ("extInfo".equals(docData.getFieldCode())
                    || "fileExtInfo".equals(docData.getFieldCode()))) {
                continue;
            }
            docDataList.add(docData);

            if (i == cachedDataList.size() - 1) {
                propertyMap.put(key, docDataList.stream().collect(Collectors.toList()));
            }
        }
        Set<String> nodes = propertyMap.keySet();
        for (String node : nodes) {
            List<DocData> list = propertyMap.get(node);
            list.forEach(x -> formatData(list, x));
        }
    }

    /**
     * 扁平化数据
     *
     * @param list
     * @param data
     */
    private void formatData(List<DocData> list, DocData data) {
        int indexOf = data.getFieldSortNo().lastIndexOf(".");
        if (indexOf > -1) {
            String parentSortNo = data.getFieldSortNo().substring(0, indexOf);
            DocData parentData = list.stream().filter(x -> x.getFieldSortNo().equals(parentSortNo)).findAny().orElse(null);
            data.setFieldCode(data.getFieldCode());
            data.setParentFieldCode(parentData.getFieldCode());
            data.setParentFieldSortNo(parentData.getFieldSortNo());
        }
    }

    public Map<String, List<DocData>> getPropertyMap() {
        return propertyMap;
    }

    public static void main(String[] args) {
        String no = "2";
        int indexOf = no.lastIndexOf(".");
        System.out.println(indexOf);
    }


}
